package com.nageoffer.shortlink.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.nageoffer.shortlink.admin.common.convention.result.Result;
import com.nageoffer.shortlink.admin.common.convention.result.Results;
import com.nageoffer.shortlink.admin.dao.entity.UserDO;
import com.nageoffer.shortlink.admin.dto.req.UserLoginRequestDTO;
import com.nageoffer.shortlink.admin.dto.req.UserRegisterRequestDTO;
import com.nageoffer.shortlink.admin.dto.req.UserUpdateRequestDTO;
import com.nageoffer.shortlink.admin.dto.resp.UserActualRespDTO;
import com.nageoffer.shortlink.admin.dto.resp.UserRespDTO;
import com.nageoffer.shortlink.admin.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.PanelUI;

/**
 * 用户管理控制层
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/shortlink/admin/v1/user")
public class UserController {

    private final UserService userService;

    /**
     * @description: 根据用户名查询用户信息
     * @author: 刘新智
     * @date: 2025/10/20 14:35
     * @param: [username]
     * @return: com.nageoffer.shortlink.admin.common.convention.result.Result<com.nageoffer.shortlink.admin.dto.resp.UserRespDTO>
     **/
    @GetMapping("/{username}")
    public Result<UserRespDTO> getUserByUsername(@PathVariable("username") String username) {
        return Results.success(userService.getUserByUsername(username));
    }

    /**
     * @description: 根据用户名查询无脱敏用户信息
     * @author: 刘新智
     * @date: 2025/10/20 14:34
     * @param: [username]
     * @return: com.nageoffer.shortlink.admin.common.convention.result.Result<com.nageoffer.shortlink.admin.dto.resp.UserActualRespDTO>
     **/
    @GetMapping("/actual/{username}")
    public Result<UserActualRespDTO> getActualUserByUsername(@PathVariable("username") String username) {
        return Results.success(BeanUtil.toBean(userService.getUserByUsername(username), UserActualRespDTO.class));
    }

    /**
     * @description: 获取当前登录用户信息
     * @author: 刘新智
     * @date: 2025/10/21
     * @param: []
     * @return: com.nageoffer.shortlink.admin.common.convention.result.Result<com.nageoffer.shortlink.admin.dto.resp.UserRespDTO>
     **/
    @GetMapping("/current")
    public Result<UserDO> getCurrentUserInfo() {
        return Results.success(userService.getCurrentUserInfo());
    }

    /**
     * @description: 判断用户名是否存在
     * @author: 刘新智
     * @date: 2025/10/19 11:00
     * @param: [username]
     * @return: com.nageoffer.shortlink.admin.common.convention.result.Result<java.lang.Boolean>
     **/
    @GetMapping("/has/{username}")
    public Result<Boolean> hasUserName(@PathVariable("username") String username){
        return Results.success(userService.hasUsername(username));
    }

    /**
     * @description: 注册用户
     * @author: 刘新智
     * @date: 2025/10/19 11:01
     * @param: [userRegisterRequestDTO]
     * @return: com.nageoffer.shortlink.admin.common.convention.result.Result<java.lang.Void>
     **/
    @PostMapping("/register")
    public Result<Void> registerUser(@RequestBody UserRegisterRequestDTO userRegisterRequestDTO){
        userService.register(userRegisterRequestDTO);
        return Results.success();
    }


    /**
     * @description: 修改用户信息
     * @author: 刘新智
     * @date: 2025/10/20 14:45
     * @param: [userRegisterRequestDTO]
     * @return: com.nageoffer.shortlink.admin.common.convention.result.Result<java.lang.Void>
     **/
    @PostMapping("/update")
    public Result<Void> updateUser(@RequestBody UserUpdateRequestDTO userUpdateRequestDTO){
        userService.update(userUpdateRequestDTO);
        return Results.success();
    }

    /**
     * @description: 用户登录
     * @author: 刘新智
     * @date: 2025/10/20 15:21
     * @param: [userLoginRequestDTO, session]
     * @return: com.nageoffer.shortlink.admin.common.convention.result.Result<com.nageoffer.shortlink.admin.dto.resp.UserRespDTO>
     **/
    @PostMapping("/login")
    public Result<UserRespDTO> passwordLogin(@RequestBody UserLoginRequestDTO userLoginRequestDTO, HttpSession session) {
        UserRespDTO userRespDTO = userService.login(userLoginRequestDTO, session);
        return Results.success(userRespDTO);
    }

    /**
     * @description: 用户登出
     * @author: 刘新智
     * @date: 2025/10/20 15:21
     * @param: [request, session]
     * @return: com.nageoffer.shortlink.admin.common.convention.result.Result<java.lang.Void>
     **/
    @PostMapping("/logout")
    public Result<Void> logout(HttpServletRequest request, HttpSession session) {
        userService.logout(session);
        return Results.success();
    }
}